<script setup lang="ts">
import { ref, onMounted } from 'vue'

import reviews from '@/assets/mock/review.json'

const cardList = ref<Array<{ text: string; top: string; left: string }>>([])

onMounted(() => {
  // 动态计算卡片位置
  const containerWidth = window.innerWidth * 2 // 双倍宽度保证滚动空间
  const cardWidth = 280 // 卡片宽度
  const horizontalGap = 40 // 水平间距
  const verticalRange = 500 // 垂直随机范围（700-200）
  
  // 生成原始位置
  let positions = []
  let currentX = 0
  for (let review of reviews) {
    positions.push({
      text: review.text,
      top: `${Math.random() * verticalRange}px`,
      left: `${currentX}px`
    })
    currentX += cardWidth + horizontalGap
  }
  
  // 生成克隆位置（保持相同随机分布）
  const cloneOffset = currentX + horizontalGap
  for (let review of reviews) {
    positions.push({
      text: review.text,
      top: `${Math.random() * verticalRange}px`,
      left: `${currentX + cloneOffset}px`
    })
    currentX += cardWidth + horizontalGap
  }

  cardList.value = positions
})
</script>

<template>
  <section class="reviews-container">
    <div class="scroller">
      <div 
        v-for="(card, index) in cardList" 
        :key="index"
        class="review-card"
        :style="{ top: card.top, left: card.left }"
      >
        <div class="stars">★★★★★</div>
        <p>{{ card.text }}</p>
      </div>
    </div>
  </section>
</template>

<style scoped lang="scss">
.reviews-container {
  height: 700px;
  overflow: hidden;
  position: relative;
  background: #f5f5f7;
}

.scroller {
  position: absolute;
  animation: scroll 40s linear infinite;
  will-change: transform;
}

.review-card {
  position: absolute;
  width: 280px;
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-5px) scale(1.02);
  }

  .stars {
    color: #ffc107;
    font-size: 1.4rem;
    margin-bottom: 12px;
  }

  p {
    font-size: 1rem;
    line-height: 1.4;
    color: #333;
    margin: 0;
  }
}

@keyframes scroll {
  from { transform: translateX(0); }
  to { transform: translateX(-50%); }
}

@media (max-width: 768px) {
  .review-card {
    width: 240px;
    padding: 15px;
  }
  
  .scroller {
    animation-duration: 30s;
  }
}
</style>